/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import share.PGHelper;

/**
 *
 * @author KieuAnh
 */
public class CFPlayWithFriend implements JSONable
{
    private CFPlayWithFriend() {}
    static CFPlayWithFriend parse(Map<String, Object> json)
    {
        CFPlayWithFriend f = new CFPlayWithFriend();
        f.deser(json);
        
        return f;
    }
    
    private HelpFish helpFish;
    private StealEgg stealEgg;
    private Snowman snowman;

    @Override
    public void deser(Map<String, Object> json)
    {
        this.helpFish = new HelpFish();
        this.getHelpFish().deser((Map) json.get("help_fish"));
        
        this.stealEgg = new StealEgg();
        this.getStealEgg().deser((Map) json.get("steal_egg"));
        
        this.snowman = new Snowman();
        this.snowman.deser((Map) json.get("snowman"));
    }

    /**
     * @return the helpFish
     */
    public HelpFish getHelpFish() {
        return helpFish;
    }

    /**
     * @return the stealEgg
     */
    public StealEgg getStealEgg() {
        return stealEgg;
    }
    
    public Snowman snowman()
    {
        return snowman;
    }
    
    public static class HelpFish implements JSONable
    {
        private  HelpFish() {}
        private int perFriend;
        private int maxFriend;
        private CFPrizePools prizePools;

        @Override
        public void deser(Map<String, Object> json)
        {
            this.perFriend = PGHelper.toInt(json.get("per_friend"));
            this.maxFriend = PGHelper.toInt(json.get("max_friend"));
            
            this.prizePools = new CFPrizePools();
            this.prizePools.deser((Map) json.get("prizePools"));
        }

        public int getPerFriend() {
            return perFriend;
        }

        public int getMaxFriend() {
            return maxFriend;
        }
        
        public CFPrizePools prizes()
        {
            return this.prizePools;
        }
    }
    
    public static class StealEgg implements JSONable
    {
        private  StealEgg() {}
        private int perFriend;
        private int maxFriend;
        private int resetTime;

        @Override
        public void deser(Map<String, Object> json)
        {
            this.perFriend = PGHelper.toInt(json.get("per_friend"));
            this.maxFriend = PGHelper.toInt(json.get("max_friend"));
            this.resetTime = PGHelper.toInt(json.get("reset_time"));
        }

        public int getPerFriend() {
            return perFriend;
        }

        public int getResetTime() {
            return resetTime;
        }

        public int getMaxFriend() {
            return maxFriend;
        }
    }
    
    public static class Snowman implements JSONable
    {
        private Snowman()
        {
            this.randomizer = CFPrizePools.makeRandomizer();
            this.harvestPrizes = new CFPrizePools.Pool(randomizer);
            this.buildPrizes = new CFPrizePools(randomizer);
        }
        
        private int maxStep;
        private int maxHelp;
        
        private final CFPrizePools.Pool harvestPrizes;
        private final CFPrizePools buildPrizes;
        
        private int totalBonusRate;
        private NavigableMap<Integer, String> bonusRates;
        
        private final SecureRandom randomizer;

        @Override
        public void deser(Map<String, Object> json) {
            maxStep = PGHelper.toInt(json.get("MaxStep"));
            maxHelp = PGHelper.toInt(json.get("MaxHelp"));
            deserHarvestPrize((Map) json.get("totalPrize"));
            deserBuildPrize((Map) json.get("stepPrizes"));
            deserBonuses((Map) json.get("bonuses"));
        }
        
        private void deserHarvestPrize(Map<String, Object> json)
        {
            this.harvestPrizes.deser(json);
        }
        
        private void deserBuildPrize(Map<String, Object> json)
        {
            buildPrizes.deser(json);
        }
        
        private void deserBonuses(Map<String, Object> json)
        {
            totalBonusRate = 0;
            bonusRates = new TreeMap();
            
            for (Map.Entry<String, Object> entry : json.entrySet()) {
                Map<String, Object> data = (Map) entry.getValue();
                
                int rate = PGHelper.toInt(data.get("rate"));
                String item = (String) data.get("item");
                
                bonusRates.put(totalBonusRate, item);
                totalBonusRate += rate;
            }
        }
        
        public int maxStep()
        {
            return this.maxStep;
        }
        
        public int maxHelp()
        {
            return this.maxHelp;
        }
        
        public CFPrizePools getBuildPrizes()
        {
            return this.buildPrizes;
        }
        
        public CFPrizePools.Pool getHarvestPrizes()
        {
            return this.harvestPrizes;
        }
        
        public String randomBonus()
        {
            int randVal = randomizer.nextInt(totalBonusRate);
            return this.bonusRates.get(this.bonusRates.floorKey(randVal));
        }
    }
}